//
//  HubuThirdViewController.m
//  iHubu2
//
//  Created by hanshui on 12-10-31.
//  Copyright (c) 2012年 hanshui. All rights reserved.
//

#import "HubuThirdViewController.h"
#import "TableCell3.h"
#import "HubuAppDelegate.h"
#import "HubuViewController.h"

@interface HubuThirdViewController ()

@end

@implementation HubuThirdViewController
@synthesize upArrowImageView, categoryView, sepLineLabel;
@synthesize listTableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)categoryClick:(id)sender {
    [upArrowImageView setHidden:NO];
    [sepLineLabel setHidden:NO];
    
    CGPoint centerPoint = categoryView.center;
    centerPoint.y = 350 + 175 - 40;
    [UIView beginAnimations:@"movement" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.2];
    categoryView.center = centerPoint;
    
    [UIView commitAnimations];
    
    UIButton *button = (UIButton *)sender;
    //NSLog(@":::%@", button.titleLabel.text);
    columnSelectIndex = button.tag;
    
    currentPage = 1;
    isLastPage = FALSE;
    currentCategoryName = button.titleLabel.text;
    
    if(bookData == nil){
        bookData = [[NSMutableArray alloc] initWithCapacity:5];
    }else{
        [bookData removeAllObjects];
    }
    
    DBUtil *db = [[DBUtil alloc] init];
    [db openDB];
    [db getCategoryData:bookData categoryName:currentCategoryName page:[NSString stringWithFormat:@"%d", currentPage] keyword:@""];
    [db closeDB];
    [db release];
    
    [listTableView reloadData];
}

-(IBAction)upArrowClick:(id)sender {
    if(!upArrowImageView.hidden) {
        [upArrowImageView setHidden:YES];
        [sepLineLabel setHidden:YES];
        CGPoint centerPoint = categoryView.center;
        centerPoint.y = 175;
        [UIView beginAnimations:@"movement" context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        [UIView setAnimationDuration:0.2];
        categoryView.center = centerPoint;
        
        [UIView commitAnimations];
    }
}


#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [bookData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    /*static NSString *CellTableIdentifier = @"CellTableIdentifier";
     
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
     CellTableIdentifier];
     if (cell == nil) {
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
     reuseIdentifier: CellTableIdentifier] autorelease];
     }
     cell.selectionStyle = UITableViewCellSelectionStyleGray;
     cell.textLabel.text = @"Text";
     */
    /*
     CategoryCount *categoryCount = [categoriesData objectAtIndex:[indexPath row]];
     cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)",
     categoryCount.categoryName,
     categoryCount.categoryCount];
     cell.textLabel.textColor = [UIColor whiteColor];*/
    //cell.imageView.image = [UIImage imageNamed:@"gz_dot_2.png"];
    static NSString *CellTableIdentifier = @"CellTableIdentifier2";
    TableCell3 *cell = (TableCell3 *)[tableView dequeueReusableCellWithIdentifier:
                                      CellTableIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"TableCell3" owner:self options:nil] objectAtIndex:0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    BookItem *bookItem = [bookData objectAtIndex:[indexPath row]];
    [cell initExtend:bookItem];
    [[cell buyButton] setTag:([[bookItem pkId] intValue] + buyBtnTagBegin)];
    [cell setTag:([[bookItem pkId] intValue] + buyBtnParentTagBegin)];
    return cell;
}

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 86;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(!isLastPage && ([indexPath row] + 1) == [bookData count]){
        HubuAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
        [delegate.hubuViewController showStatePad:@"" msg:@"加载更多图书..." interval:3];
        currentPage++;

        DBUtil *db = [[DBUtil alloc] init];
        [db openDB];
        [db getCategoryData:bookData categoryName:currentCategoryName page:[NSString stringWithFormat:@"%d", currentPage] keyword:@""];
        [db closeDB];
        [db release];
        
        //如果不是21（分页）的整数倍，则到末页
        if([bookData count] % 21 > 0){
            isLastPage = TRUE;
        }
        
        [listTableView reloadData];
    }
}
@end
